﻿using equinoxWeb.GestionConexion;
using equinoxWeb.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace equinoxWeb.Controllers
{
    public class TareaController : EquinoxController
    {
        //
        // GET: /Tarea/

        public ActionResult MisTareas()
        {
            if (defaultAction != null) return defaultAction;
            if (esCliente)
                return View("~/Views/Shared/PermisoDenegado.cshtml");
            Usuario user = CuentaController.getUsuario(this.HttpContext);
            int idUsuario = user.IdUsuario;
            List<Tarea> tareas = TareaFunction.listarTareasPendientes(idUsuario);
            ViewBag.tareasPendientes = tareas;
            return View();
        }

        public ActionResult ListarTareasCaso(int idCaso)
        {
            if (defaultAction != null) return defaultAction;
            if (esCliente)
                return View("~/Views/Shared/PermisoDenegado.cshtml");
            Usuario user = CuentaController.getUsuario(this.HttpContext);
            List<Tarea> tareas = TareaFunction.listarTareasCaso(idCaso);
            ViewBag.idCaso = idCaso;
            ViewBag.tareasPendientes = tareas;
            return View();
        }

        public ActionResult Create(int idCaso)
        {
            if (defaultAction != null) return defaultAction;
            if (esCliente)
                return View("~/Views/Shared/PermisoDenegado.cshtml");
            List<Usuario> usuarios = UsuarioFunction.listarUsuario();
            ViewBag.listaUsuario = usuarios;

            ViewBag.idUsuarioConectado = CuentaController.getUsuario(this.HttpContext).IdUsuario;

            ViewBag.idCaso = idCaso;
            return View();
        }

        [HttpPost]
        public ActionResult Create(Tarea tarea,int idCaso)
        {
            if (defaultAction != null) return defaultAction;
            if (esCliente)
                return View("~/Views/Shared/PermisoDenegado.cshtml");
            tarea.IdCaso = idCaso;
            TareaFunction.agregarTarea(tarea);

            RouteValueDictionary routeValues = new RouteValueDictionary();
            routeValues.Add("idCaso", tarea.IdCaso);
            routeValues.Add("idTarea", tarea.IdTarea);
            return RedirectToAction("ListarTareasCaso", routeValues);
        }

        public ActionResult Search()
        {
            if (defaultAction != null) return defaultAction;
            if (esCliente)
                return View("~/Views/Shared/PermisoDenegado.cshtml");

            return View();
        }
        [HttpPost]
        public ActionResult AtenderTarea(string accionOrigen,int idTarea,int idCaso)
        {
            if (defaultAction != null) return defaultAction;
            if (esCliente)
                return View("~/Views/Shared/PermisoDenegado.cshtml");
            RouteValueDictionary routeValues = new RouteValueDictionary();
            routeValues.Add("idCaso", idCaso);
            routeValues.Add("idTarea", idTarea);
            TareaFunction.comenzarTarea(idTarea, idCaso);
            return RedirectToAction(accionOrigen, routeValues);
        }
        [HttpPost]
        public ActionResult FinalizarTarea(string accionOrigen, int idTarea, int idCaso)
        {
            if (defaultAction != null) return defaultAction;
            if (esCliente)
                return View("~/Views/Shared/PermisoDenegado.cshtml");
            RouteValueDictionary routeValues = new RouteValueDictionary();
            routeValues.Add("idCaso", idCaso);
            routeValues.Add("idTarea", idTarea);
            TareaFunction.finalizarTarea(idTarea, idCaso);
            return RedirectToAction(accionOrigen, routeValues);
        }

    }
}
